﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.IO;

namespace com.sharepointutils.application
{
    public class Query
    {

        public static Boolean ContainsElement(String xml, String search)
        {
            XDocument doc = XDocument.Parse(xml);
            IEnumerable<XElement> elements = doc.Elements(search);
            return elements.Count() > 0;
        }

        public static String InsertElement(String xml, String element, String value)
        {
            String returnXML = xml;
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            XmlNode root = doc.DocumentElement;
            XmlElement elem = doc.CreateElement(element);
            elem.InnerText = value;
            root.AppendChild(elem);

            using (var stringWriter = new StringWriter())
            {
                using (var xmlTextWriter = XmlWriter.Create(stringWriter))
                {
                    doc.WriteTo(xmlTextWriter);
                    xmlTextWriter.Flush();
                    returnXML = stringWriter.GetStringBuilder().ToString();
                }
            }
            return returnXML;
        }

    }
}
